package com.example.itcacp.repository.mybatis;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.itcacp.entity.Like;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 点赞Mapper层
 * */
@Repository
public interface LikeMapper extends BaseMapper<Like> {

    /**
     * 查询对应内容点赞数
     * COUNT（字段）: 计数
     * GROUP_CONCAT(字段) : 合并
     * */
    @Select ( "SELECT COUNT(*) FROM itcacp.pc_like WHERE " +
            "target_id = #{contentId} AND target_type = #{contentType}" )
    Long findLikes (Long contentId, Like.ContentType contentType);


    /**
     * 查询用户是否已点赞
     */
    @Select("SELECT COUNT(*) FROM pc_like " +
            "WHERE user_id = #{userId} AND target_id = #{targetId} AND target_type = #{targetType}")
    Integer isLiked(@Param("userId") Long userId,
                    @Param("targetId") Long targetId,
                    @Param("targetType") Like.ContentType targetType);

    /**
     * 查询点赞记录
     * */
    @Select ( "SELECT * FROM pc_like " +
            "WHERE user_id = #{userId} AND target_id = #{targetId} AND target_type = #{targetType}" )
    Like findLikeId ( Long userId, Long targetId, Like.ContentType targetType);

    /**
     * 查询所有点赞记录
     * */
    @Select ("SELECT * FROM pc_like")
    List<Like> selectAllLikes();

    /**
     * 存在点过赞的所有用户
     * DISTINCT ：查询出相同的数据组合为一条
     * */
    @Select ( "SELECT DISTINCT user_id FROM pc_like" )
    List<Long> getAllUserIds();

    /**
     * 对应用户的所有点赞记录
     * */
    @Select ( "SELECT * FROM pc_like WHERE user_id = #{userId}" )
    List<Like> getLikesByUserId(@Param("userId") Long userId);
}
